package service

import (
	"github.com/astaxie/beego/orm"
	"net/url"
	"strings"
	"xianzhi/models"
	"xianzhi/utils"
)

// 获取订单推送时间信息
func GetOrderPushTimeInfo(p map[string]interface{}) *models.OrderPushTime {
	o := orm.NewOrm()
	orderPushTime := &models.OrderPushTime{}
	qs := o.QueryTable("OrderPushTime")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(orderPushTime)
	return orderPushTime
}

// 添加单个订单推送时间
func AddOrderPushTime(input url.Values) (int64, error) {
	o := orm.NewOrm()
	orderPushTime := &models.OrderPushTime{}
	order := &models.Order{}
	if _, ok := input["order_id"]; ok {
		order.Id = utils.Atoi64(input["order_id"][0])
		o.Read(order)
	}
	orderPushTime.Order = order
	pushTime := &models.PushTime{}
	if _, ok := input["push_time_id"]; ok {
		pushTime.Id = utils.Atoi64(input["push_time_id"][0])
		o.Read(pushTime)
	}
	orderPushTime.PushTime = pushTime
	if pushTime.Id > 0 {
		orderPushTime.PushTimeMonth = pushTime.Month
		orderPushTime.PushTimePublication = pushTime.Publication
		orderPushTime.PushTimeNum = pushTime.Num
	}
	return o.Insert(orderPushTime)
}

// 添加多个订单推送时间
func AddOrderPushTimes(orderId int64, pushTimeIds string) (int64, error) {
	o := orm.NewOrm()
	order := &models.Order{}
	order.Id = orderId
	o.Read(order)
	var orderPushTimes []models.OrderPushTime
	pushTimeIdArr := strings.Split(pushTimeIds, ",")
	for _, pushTimeId := range pushTimeIdArr {
		pushTime := models.PushTime{}
		pushTime.Id = utils.Atoi64(pushTimeId)
		o.Read(&pushTime)
		orderPushTime := models.OrderPushTime{}
		orderPushTime.Order = order
		orderPushTime.PushTime = &pushTime
		orderPushTime.PushTimeMonth = pushTime.Month
		orderPushTime.PushTimePublication = pushTime.Publication
		orderPushTime.PushTimeNum = pushTime.Num
		orderPushTimes = append(orderPushTimes, orderPushTime)
	}
	return o.InsertMulti(1, orderPushTimes)
}

// 修改订单推送时间信息
func EditOrderPushTime(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("OrderPushTime").Filter("id", input["id"][0]).Update(p)
}

// 获取订单推送时间列表
func GetOrderPushTimeList(p map[string]interface{}) (int64, []*models.OrderPushTime) {
	var orderPushTimeList []*models.OrderPushTime
	o := orm.NewOrm()
	qs := o.QueryTable("OrderPushTime")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["orderId"]; ok {
		qs = qs.Filter("order_id", p["orderId"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&orderPushTimeList)
	return totalCount, orderPushTimeList
}

func DelOrderPushTime(p map[string]interface{}) (int64, error) {
	if len(p) < 1 {
		return -1, nil
	}
	o := orm.NewOrm()
	qs := o.QueryTable("OrderPushTime")
	if _, ok := p["orderId"]; ok {
		qs = qs.Filter("order_id", p["orderId"])
	}
	return qs.Delete()
}